package mypackage3;

import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringJoiner;

public class ArrayListTest {
    public static void main(String[] args) {
        //用ArrayList存字符串并输出
        Scanner sc=new Scanner(System.in);
        System.out.println("您要输入多少个字符串?");
        int num=sc.nextInt();
        ArrayList<String> list=new ArrayList<>();
        for (int i = 0; i < num; i++) {
            System.out.println("请输入第"+(i+1)+"个字符串");
            list.add(sc.next());
        }
        DirectPrintList(list);
        InDirectPrintList(list);
        InDirectPrintList2(list);

    }
    public static void DirectPrintList(ArrayList<String> list){
        System.out.println(list);
    }
    public static void InDirectPrintList(ArrayList<String> list){
        StringBuilder sb=new StringBuilder();
        sb.append('[');
        for (int i = 0; i < list.size(); i++) {
            sb.append(list.get(i));
            if(i!=list.size()-1){
                sb.append(", ");
            }
        }
        sb.append(']');
        System.out.println(sb);
    }
    public static void InDirectPrintList2(ArrayList<String> list){
        StringJoiner sj=new StringJoiner(", ","[","]");
        for (int i = 0; i < list.size(); i++) {
            sj.add(list.get(i));
        }
        System.out.println(sj);
    }
}
